package com.itmk.business.articleCategory.controller;


import com.itmk.business.article.entity.Article;
import com.itmk.business.article.service.ArticleService;
import com.itmk.business.articleCategory.entity.ArticleCategory;
import com.itmk.business.articleCategory.service.ArticleCategoryService;
import com.itmk.result.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/article")
@Api(tags = "文章分类管理")
public class ArticleCategoryController {
        @Autowired
        private ArticleCategoryService articleCategoryService;
        @Autowired
        private ArticleService articleService;

    @ApiOperation(value = "查询所有分类")
    @RequestMapping(value = "/getcategorylist",method = RequestMethod.POST)
    public DataResult getcategorylist(){
        List<ArticleCategory> articleCategoryList=articleCategoryService.list();
        return DataResult.success(articleCategoryList);
    }

    @ApiOperation(value = "根据Id查询文章分类")
    @RequestMapping(value = "/getcategorybyid",method = RequestMethod.POST)
    public DataResult getCategoryById(@RequestBody ArticleCategory articleCategory){
        ArticleCategory category=articleCategoryService.getById(articleCategory.getId());
        return DataResult.success(category);
    }

    @ApiOperation(value = "保存文章分类")
    @RequestMapping(value = "/savecategory",method = RequestMethod.POST)
    public DataResult savecategory(@RequestBody ArticleCategory articleCategory){
        ArticleCategory category=articleCategoryService.getCategoryByName(articleCategory.getCategory());
        if (category!=null){
            return DataResult.getResult(300100,"保存失败，分类列表已存在!");
        }
        Boolean b=articleCategoryService.save(articleCategory);
        if(b){
            return DataResult.success();
        }else{
            return DataResult.getResult(300100,"保存失败!");
        }
    }

    @ApiOperation(value = "编辑文章分类")
    @RequestMapping(value = "/editcategory",method = RequestMethod.POST)
    public DataResult editcategory(@RequestBody ArticleCategory articleCategory){
        Boolean b=articleCategoryService.updateById(articleCategory);
        if(b){
            return DataResult.success();
        }else{
            return DataResult.getResult(300100,"编辑失败!");
        }
    }

    @ApiOperation(value = "删除文章分类")
    @RequestMapping(value = "/deletecategory",method = RequestMethod.POST)
    public DataResult deletecategory(@RequestBody ArticleCategory articleCategory){

        List<Article> articles=articleService.getArticlesByCategoryId(articleCategory.getId());
        if (articles !=null && articles.size()>0 ){
            return DataResult.getResult(300100,"删除失败，当前分类文章列表不为空！");
        }
        Boolean b=articleCategoryService.removeById(articleCategory.getId());
        if(b){
            return DataResult.success();
        }else{
            return DataResult.getResult(300100,"编辑失败!");
        }
    }

}
